package com.freeway.image.combiner.element;

import com.freeway.image.combiner.enums.LineAlign;
import com.freeway.image.combiner.utils.ElementUtil;
import sun.font.FontDesignMetrics;

import java.awt.*;

/**
 * @Author zhaoqing.chen
 * @Date 2020/8/21
 * @Description 文本元素
 **/

public class TextElement extends CombineElement<TextElement> {

    //基础属性
    private String text;                //文本
    private Font font;                  //字体
    private boolean strikeThrough;      //删除线
    private Color color = new Color(0, 0, 0);   //颜色，默认黑色
    private Integer rotate;             //旋转
    private Integer lineHeight;         //行高（根据设计稿具体值设置，若不设置，则以文字的ascent基线计算文字高度）
    private Integer width;              //宽度
    private Integer designY;            //跟UI设计软件一致的y坐标

    //换行计算相关属性
    private boolean autoBreakLine = false;          //是否自动换行
    private int maxLineWidth = 600;                 //最大行宽，超出则换行
    private int maxLineCount = 2;                   //最大行数，超出则丢弃多余行
    private LineAlign lineAlign = LineAlign.Left;   //行对齐方式，默认左对齐

    /**
     * @param text 文本内容
     * @param font Font对象
     * @param x    x坐标
     * @param y    y坐标
     */
    public TextElement(String text, Font font, int x, int y) {
        this.text = text;
        this.font = font;
        super.setX(x);
        super.setY(y);
    }

    /**
     * @param text     文本内容
     * @param fontSize 字号
     * @param x        x坐标
     * @param y        y坐标
     */
    public TextElement(String text, int fontSize, int x, int y) {
        this.text = text;
        this.font = new Font("阿里巴巴普惠体", Font.PLAIN, fontSize);
        super.setX(x);
        super.setY(y);
    }

    /**
     * @param text     文本内容
     * @param fontName 字体名称
     * @param fontSize 字号
     * @param x        x坐标
     * @param y        y坐标
     */
    public TextElement(String text, String fontName, int fontSize, int x, int y) {
        this.text = text;
        this.font = new Font(fontName, Font.PLAIN, fontSize);
        super.setX(x);
        super.setY(y);
    }


    public String getText() {
        return text;
    }

    public TextElement setText(String text) {
        this.text = text;
        return this;
    }

    public Font getFont() {
        return font;
    }

    public TextElement setFont(Font font) {
        this.font = font;
        return this;
    }

    public Integer getRotate() {
        return rotate;
    }

    public TextElement setRotate(Integer rotate) {
        this.rotate = rotate;
        return this;
    }

    public Integer getLineHeight() {
        return lineHeight;
    }

    public TextElement setLineHeight(Integer lineHeight) {
        this.lineHeight = lineHeight;
        return this;
    }

    public Color getColor() {
        return color;
    }

    public TextElement setColor(Color color) {
        this.color = color;
        return this;
    }

    public TextElement setColor(int r, int g, int b) {
        return setColor(new Color(r, g, b));
    }

    public Integer getWidth() {
        if (width == null) {
            width = ElementUtil.computeTextWidth(text, font);   //延迟计算
        }
        return width;
    }

    public Integer getDesignY() {
        if (designY == null) {
            if (lineHeight != null) {
                designY = getY() + (lineHeight - this.computeTextHeight(font)) / 2 + this.computeTextAscent(font);
            } else {
                designY = getY() + this.computeTextAscent(font);
            }
        }
        return designY;
    }

    public TextElement setDesignY(Integer designY) {
        this.designY = designY;
        return this;
    }

    public boolean isStrikeThrough() {
        return strikeThrough;
    }

    public TextElement setStrikeThrough(boolean strikeThrough) {
        this.strikeThrough = strikeThrough;
        return this;
    }

    public boolean isAutoBreakLine() {
        return autoBreakLine;
    }

    /**
     * 设置自动换行（默认左对齐）
     *
     * @param maxLineWidth 最大宽度（超出则换行）
     * @param maxLineCount 最大行数（超出则丢弃）
     * @param lineHeight   行高
     * @return
     */
    public TextElement setAutoBreakLine(int maxLineWidth, int maxLineCount, int lineHeight) {
        this.autoBreakLine = true;
        this.maxLineWidth = maxLineWidth;
        this.maxLineCount = maxLineCount;
        this.lineHeight = lineHeight;
        return this;
    }

    /**
     * 设置自动换行
     *
     * @param maxLineWidth 最大宽度（超出则换行）
     * @param maxLineCount 最大行数（超出则丢弃）
     * @param lineHeight   行高
     * @param lineAlign    行对齐方式
     * @return
     */
    public TextElement setAutoBreakLine(int maxLineWidth, int maxLineCount, int lineHeight, LineAlign lineAlign) {
        this.autoBreakLine = true;
        this.maxLineWidth = maxLineWidth;
        this.maxLineCount = maxLineCount;
        this.lineHeight = lineHeight;
        this.lineAlign = lineAlign;
        return this;
    }

    public int getMaxLineWidth() {
        return maxLineWidth;
    }

    public int getMaxLineCount() {
        return maxLineCount;
    }

    public LineAlign getLineAlign() {
        return lineAlign;
    }

    /**
     * 计算文本高度
     * @param font
     * @return
     */
    private int computeTextHeight(Font font) {
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        return metrics.getHeight();
    }

    /**
     * 计算文本上部高度（基线base到ascent）
     * @param font
     * @return
     */
    private int computeTextAscent(Font font) {
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        return metrics.getAscent();
    }
}
